package com.team.lostProperty.controller;

import com.google.gson.Gson;
import com.team.lostProperty.entity.*;
import com.team.lostProperty.service.CommentsService;
import com.team.lostProperty.service.NewPushInfoService;
import com.team.lostProperty.service.PushInfoService;
import com.team.lostProperty.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.applet.Main;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author duzq
 * @date 2020/7/22 19:02
 */

@RestController
@RequestMapping("/comments")
public class CommentsController {

    private IdWorker idWorker = new IdWorker(1, 1, 1);

    @Resource
    private NewPushInfoService newPushInfoService;

    @Resource
    private CommentsService commentsService;

    @Resource
    private PushInfoService pushInfoService;

    Result result = new Result();


    /**
     * 获取评论
     *
     * @param status
     * @param page
     * @param limit
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/getComments")
    private Result getComments(String status, String page, String limit) {
        List<Comments> commentsList = commentsService.getComments(Integer.valueOf(status), Integer.valueOf(page), Integer.valueOf(limit));
        if (commentsList.size() >= 0) {
            result.setCode(0);
            result.setMsg("成功");
            result.setCount(commentsService.getAllCount(Integer.valueOf(status)));
            result.setData(commentsList);
        } else {
            result.setCode(1);
            result.setMsg("失败");
            result.setCount(0);
            result.setData("");
        }
        return result;
    }

    /**
     * 获取评论
     *
     * @param
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/selectByPushInfoId")
    private Result selectByPushInfoId(@RequestParam Map<String, String> PushInfoIdMap) {
        String pushInfoId = PushInfoIdMap.get("pushInfoId");
        Long id = Long.parseLong(pushInfoId);
        try {
            List<Comments> comments = commentsService.selectByPushInfoId(id);
            Gson gson = new Gson();
            String date = gson.toJson(comments);
            result.setCode(0);
            result.setMsg("成功");
            result.setData(date);
            result.setCount(comments.size());
        } catch (Exception e) {

        }
        return result;
    }

    /**
     * 插入评论
     *
     * @param
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/insert")
    private Result insert(@RequestParam Map<String, String> commentMap) {
        String createdDate = commentMap.get("createdDate");
        long userId = Long.parseLong(commentMap.get("userId"));
        long pushInfoId = Long.parseLong(commentMap.get("pushInfoId"));
        String content = commentMap.get("content");
        Comments date = commentsService.insertComment(userId, pushInfoId, createdDate, content);
       if (date!=null){
           insertNewPushInfo(date);
       }
        result.setCode(0);
        result.setMsg("评论成功");
        result.setData(date);
        result.setCount(0);
        return result;
    }

    /**
     * 根据条件查询评论
     *
     * @param request
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/selectComments")
    private Result selectComments(HttpServletRequest request) {
        String status = request.getParameter("status");
        Comments comments = new Comments();
        User user = new User();
        PushInfo pushInfo = new PushInfo();

        if (!request.getParameter("id").equals("")) {
            comments.setCommentsId(Integer.valueOf(request.getParameter("id")));
        }
        user.setUserName(request.getParameter("poster"));
        comments.setUser(user);
        pushInfo.setTitle(request.getParameter("title"));
        comments.setStatus(Integer.valueOf(status));
        comments.setPushInfo(pushInfo);

        List<Comments> commentsList = commentsService.selectComments(comments);
        if (commentsList.size() >= 0) {
            result.setCode(0);
            result.setMsg("成功");
            result.setData(commentsList);
            result.setCount(commentsService.getAllCount(Integer.valueOf(status)));
        } else {
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
            result.setCount(0);
        }
        return result;
    }

    /**
     * 根据ID删除评论
     *
     * @param ids
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/deleteComments")
    private Result deleteComments(String ids) {
        int i = 0;
        if (ids != null && !ids.equals("")) {
            String[] str = ids.split(",");
            for (String id : str) {
                if (id != null && !id.equals("")) {
                    i = commentsService.deleteComments(Integer.valueOf(id));
                }
            }
        }
        if (i > 0) {
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        } else {
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }

    /**
     * 修改评论状态
     *
     * @param commentsId
     * @param status
     * @return Result
     */
    @CrossOrigin
    @RequestMapping("/updateComments")
    private Result updateComments(long commentsId, int status) {
        int i = commentsService.updateComments(status, commentsId);
        if (i > 0) {
            result.setCode(0);
            result.setMsg("成功");
            result.setData("");
        } else {
            result.setCode(1);
            result.setMsg("失败");
            result.setData("");
        }
        return result;
    }

    /**
     * 添加评论消息
     *
     * @param comments
     */
    public void insertNewPushInfo(Comments comments) {
        NewPushInfo newPushInfo = new NewPushInfo();
        newPushInfo.setNewPushInfoId(idWorker.nextId());
        newPushInfo.setPushInfo(comments.getPushInfo());
        newPushInfo.setUser(pushInfoService.getPushInfoById(comments.getPushInfo().getPushInfoId()).getUser());
        List<NewPushInfo> list = newPushInfoService.selectAll(newPushInfo);
        if (list != null && list.size() > 0) {
            newPushInfo.setNewPushInfoId(list.get(0).getNewPushInfoId());
            newPushInfo.setSize(list.get(0).getSize() + 1);
            newPushInfoService.update(newPushInfo);
        } else {
            newPushInfo.setSize(1);
            newPushInfoService.insert(newPushInfo);
        }
    }

}
